package com.hf.eagle.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hf.eagle.dao.AccessoryDao;
import com.hf.eagle.dao.AccessoryRelDao;
import com.hf.eagle.dao.SaleGridDao;
import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.AccessoryRel;
import com.hf.eagle.domain.SaleGrid;
import com.hf.eagle.service.SaleGridService;

/**
 * @author Xu,XiaoYuan
 * @version 2011-09-06 23:44
 */
@Service
public class SaleGridServiceImpl implements SaleGridService {

	@Resource
	private SaleGridDao saleGridDao;

	@Resource
	private AccessoryDao accessoryDao;

	@Resource
	private AccessoryRelDao accessoryRelDao;

	public Long createSaleGrid(SaleGrid t) {
		String mod_id = (String) t.getMap().get("mod_id");

		Long grid_id = this.saleGridDao.insertEntity(t);

		List<Accessory> accList = t.getAccessoryList();
		if (null != accList && accList.size() > 0) {
			for (Accessory accessory : accList) {
				Long acce_id = accessoryDao.insertEntity(accessory);

				AccessoryRel accessoryRel = new AccessoryRel();
				accessoryRel.setMod_id(Long.valueOf(mod_id));
				accessoryRel.setRecord_id(grid_id);
				accessoryRel.setAcce_id(acce_id);

				accessoryRelDao.insertEntity(accessoryRel);
			}
		}

		return grid_id;
	}

	public SaleGrid getSaleGrid(SaleGrid t) {
		return this.saleGridDao.selectEntity(t);
	}

	public Long getSaleGridCount(SaleGrid t) {
		return this.saleGridDao.selectEntityCount(t);
	}

	public List<SaleGrid> getSaleGridList(SaleGrid t) {
		return this.saleGridDao.selectEntityList(t);
	}

	public int modifySaleGrid(SaleGrid t) {
		List<Accessory> accList = t.getAccessoryList();
		if (null != accList && accList.size() > 0) {
			/************ 删除原先的附件 ************/
			AccessoryRel acce_rel = new AccessoryRel();
			acce_rel.setMod_id(Long.valueOf((String) t.getMap().get("mod_id")));
			acce_rel.setRecord_id(t.getGrid_id());
			List<AccessoryRel> list = accessoryRelDao.selectEntityList(acce_rel);

			String[] pks = null;
			if (null != list && list.size() > 0) {
				pks = new String[list.size()];
				for (int i = 0; i < list.size(); i++) {
					pks[i] = String.valueOf(list.get(i).getAcce_id());

					accessoryRelDao.deleteEntity(list.get(i));
				}
			}

			if (null != pks && pks.length > 0) {
				Accessory acce = new Accessory();
				acce.getMap().put("pks", pks);
				accessoryDao.deleteEntity(acce);
			}
			/************ 删除原先的附件 ************/

			for (Accessory accessory : accList) {
				Long acce_id = accessoryDao.insertEntity(accessory);

				AccessoryRel accessoryRel = new AccessoryRel();
				accessoryRel.setMod_id(Long.valueOf((String) t.getMap().get("mod_id")));
				accessoryRel.setRecord_id(t.getGrid_id());
				accessoryRel.setAcce_id(acce_id);

				accessoryRelDao.insertEntity(accessoryRel);
			}
		} else if (null != t.getMap().get("acce_id") && !"".equals((String) t.getMap().get("acce_id"))) { // 修改原附件相关信息(标题等)
			String mod_id = (String) t.getMap().get("mod_id");
			String description = (String) t.getMap().get("description");

			Accessory acce = new Accessory();
			acce.setDescription(description);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", t.getGrid_id());

			accessoryDao.updateEntity(acce);
		}

		return this.saleGridDao.updateEntity(t);
	}

	public int removeSaleGrid(SaleGrid t) {
		return this.saleGridDao.updateEntity(t);
	}

	public List<SaleGrid> getSaleGridPaginatedList(SaleGrid t) {
		return this.saleGridDao.selectEntityPaginatedList(t);
	}

}
